AC_INIT(comoro, 0.1.0, conan.np@gmail.com)

AM_INIT_AUTOMAKE(comoro, 0.1.0)
AC_CONFIG_HEADERS([config.h])
AM_MAINTAINER_MODE

AC_ISC_POSIX
AC_PROG_CC
AM_PROG_CC_STDC
AC_HEADER_STDC

AM_PROG_LIBTOOL

saved_CFLAGS="$CFLAGS"
CFLAGS="-O3"

# Check libevent libmysqlclient and liblua
trylibeventdir=""
AC_ARG_WITH(event, 
	[  --with-event=PATH       Special path to libevent installation], 
	[
	 	if test "x$withval" != "xno"; then
			trylibeventdir=$withval
		fi
	]
)

AC_CACHE_CHECK([for libevent directory], ac_cv_libevent_dir, [
	saved_LIBS="$LIBS"
	saved_LDFLAGS="$LDFLAGS"
	saved_CPPFLAGS="$CPPFLAGS"
	le_found=no
	
	for ledir in $trylibeventdir "" $prefix /usr /usr/local; do
		LDFLAGS="$saved_LDFLAGS"
		LIBS="$saved_LIBS -levent"

		if test ! -z "$ledir" -a ! -d "$ledir"; then
			continue;
		fi
		if test ! -z "$ledir"; then
			if test -d "$ledir/lib"; then
				LDFLAGS="-L$ledir/lib $LDFLAGS"
			else
				LDFLAGS="-L$ledir $LDFLAGS"
			fi
			if test -d "$ledir/include"; then
				CPPFLAGS="-I$ledir/include $CPPFLAGS"
			else
				CPPFLAGS="-I$ledir $CPPFLAGS"
			fi
		fi

		AC_TRY_LINK([#include <sys/time.h>
					 #include <sys/types.h>
					 #include <event.h>], [event_init();],
					 [libevent_linked=yes], [libevent_linked=no])
		if test $libevent_linked = yes; then
			if test ! -z "$ledir"; then
				ac_cv_libevent_dir=$ledir
			else
				ac_cv_libevent_dir="(system)"
			fi
			le_found=yes
			break
		fi
	done
	LIBS="$saved_LIBS"
	LDFLAGS="$saved_LDFLAGS"
	CPPFLAGS="$saved_CPPFLAGS"

	if test $le_found = no; then
		AC_MSG_ERROR([libevent is required!])
	fi
])

LIBS="$LIBS -levent"
if test $ac_cv_libevent_dir != "(system)"; then
	if test -d "$ac_cv_libevent_dir/lib"; then
		LDFLAGS="-L$ac_cv_libevent_dir/lib $LDFLAGS"
		le_libdir="$ac_cv_libevent_dir/lib"
	else
		LDFLAGS="-L$ac_cv_libevent_dir $LDFLAGS"
		le_libdir="$ac_cv_libevent_dir"
	fi
	if test -f "$ac_cv_libevent_dir/include/event.h"; then
		CPPFLAGS="-I$ac_cv_libevent_dir/include $CPPFLAGS"
	else
		CPPFLAGS="-I$ac_cv_libevent_dir $CPPFLAGS"
	fi
fi

trylibmysqlclientdir=""
AC_ARG_WITH(mysql, 
	[  --with-mysql=PATH       Special path to MySQL installation], 
	[
	 	if test "x$withval" != "xno"; then
			trylibmysqlclientdir=$withval
		fi
	]
)

AC_CACHE_CHECK([for libmysqlclient directory], ac_cv_libmysqlclient_dir, [
	saved_LIBS="$LIBS"
	saved_LDFLAGS="$LDFLAGS"
	saved_CPPFLAGS="$CPPFLAGS"
	lm_found=no
	
	for lmdir in $trylibmysqlclientdir "" $prefix /usr /usr/local; do
		LDFLAGS="$saved_LDFLAGS"
		LIBS="$saved_LIBS -lmysqlclient"

		if test ! -z "$lmdir" -a ! -d "$lmdir"; then
			continue;
		fi
		if test ! -z "$lmdir"; then
			if test -d "$lmdir/lib/mysql"; then
				LDFLAGS="-L$lmdir/lib/mysql $LDFLAGS"
			else
				LDFLAGS="-L$lmdir $LDFLAGS"
			fi
			if test -f "$lmdir/include/mysql/mysql.h"; then
				CPPFLAGS="-I$lmdir/include/mysql $CPPFLAGS"
			else
				CPPFLAGS="-I$lmdir $CPPFLAGS"
			fi
		fi

		AC_TRY_LINK([	#include <stdlib.h>
				#include <mysql.h>], [mysql_init(NULL);],
					 [libmysqlclient_linked=yes], [libmysqlclient_linked=no])
		if test $libmysqlclient_linked = yes; then
			if test ! -z "$lmdir"; then
				ac_cv_libmysqlclient_dir=$lmdir
			else
				ac_cv_libmysqlclient_dir="(system)"
			fi
			lm_found=yes
			break
		fi
	done
	LIBS="$saved_LIBS"
	LDFLAGS="$saved_LDFLAGS"
	CPPFLAGS="$saved_CPPFLAGS"

	if test $lm_found = no; then
		AC_MSG_ERROR([libmysqlclient is required!])
	fi
])

LIBS="$LIBS -lmysqlclient"
if test $ac_cv_libmysqlclient_dir != "(system)"; then
	if test -d "$ac_cv_libmysqlclient_dir/lib/mysql"; then
		LDFLAGS="-L$ac_cv_libmysqlclient_dir/lib/mysql $LDFLAGS"
		lm_libdir="$ac_cv_libmysqlclient_dir/lib/mysql"
	else
		LDFLAGS="-L$ac_cv_libmysqlclient_dir $LDFLAGS"
		lm_libdir="$ac_cv_libmysqlclient_dir"
	fi
	if test -f "$ac_cv_libmysqlclient_dir/include/mysql/mysql.h"; then
		CPPFLAGS="-I$ac_cv_libmysqlclient_dir/include/mysql $CPPFLAGS"
	else
		CPPFLAGS="-I$ac_cv_libmysqlclient_dir $CPPFLAGS"
	fi
fi

trylibluadir=""
AC_ARG_WITH(lua, 
	[  --with-lua=PATH         Special path to liblua installation], 
	[
	 	if test "x$withval" != "xno"; then
			trylibluadir=$withval
		fi
	]
)

AC_CACHE_CHECK([for liblua directory], ac_cv_liblua_dir, [
	saved_LIBS="$LIBS"
	saved_LDFLAGS="$LDFLAGS"
	saved_CPPFLAGS="$CPPFLAGS"
	ll_found=no
	
	for lldir in $trylibluadir "" $prefix /usr /usr/local; do
		LDFLAGS="$saved_LDFLAGS"
		LIBS="$saved_LIBS -llua -lm -ldl"

		if test ! -z "$lldir" -a ! -d "$lldir"; then
			continue;
		fi
		if test ! -z "$lldir"; then
			if test -d "$lldir/lib"; then
				LDFLAGS="-L$lldir/lib $LDFLAGS"
			else
				LDFLAGS="-L$lldir $LDFLAGS"
			fi
			if test -f "$lldir/include/lua.h"; then
				CPPFLAGS="-I$lldir/include $CPPFLAGS"
			else
				if test -f "$lldir/include/lua5.1/lua.h"; then
					CPPFLAGS="-I$lldir/include/lua5.1 $CPPFLAGS"
				else
					CPPFLAGS="-I$lldir $CPPFLAGS"
				fi
			fi
		fi

		AC_TRY_LINK([#include <lua.h>
					 #include <stdio.h>], [lua_State *L = luaL_newstate(); lua_close(L);],
					 [liblua_linked=yes], [liblua_linked=no])
		if test $liblua_linked = yes; then
			if test ! -z "$lldir"; then
				ac_cv_liblua_dir=$lldir
			else
				ac_cv_liblua_dir="(system)"
			fi
			ll_found=yes
			break
		fi
	done
	LIBS="$saved_LIBS"
	LDFLAGS="$saved_LDFLAGS"
	CPPFLAGS="$saved_CPPFLAGS"

	if test $ll_found = no; then
		AC_MSG_ERROR([liblua is required!])
	fi
])

LIBS="$LIBS -llua -lm -ldl"
if test $ac_cv_liblua_dir != "(system)"; then
	if test -d "$ac_cv_liblua_dir/lib"; then
		LDFLAGS="-L$ac_cv_liblua_dir/lib $LDFLAGS"
		le_libdir="$ac_cv_liblua_dir/lib"
	else
		LDFLAGS="-L$ac_cv_liblua_dir $LDFLAGS"
		le_libdir="$ac_cv_liblua_dir"
	fi
	if test -f "$ac_cv_liblua_dir/include/lua.h"; then
		CPPFLAGS="-I$ac_cv_liblua_dir/include $CPPFLAGS"
	else
		if test -f "$ac_cv_liblua_dir/include/lua5.1/lua.h"; then
			CPPFLAGS="-I$ac_cv_liblua_dir/include/lua5.1 $CPPFLAGS"
		else
			CPPFLAGS="-I$ac_cv_liblua_dir $CPPFLAGS"
		fi
	fi
fi

# Add POSIX thread support
#LIBS="$LIBS -lpthread"

dnl --------------------------------------------------------------------------------------------------------

AC_SEARCH_LIBS(socket, socket)
AC_SEARCH_LIBS(gethostbyname, nsl)
AC_SEARCH_LIBS(mallinfo, malloc)
AC_SEARCH_LIBS(pthread_create, pthread)

AC_CHECK_HEADER(malloc.h, AC_DEFINE(HAVE_MALLOC_H,,[do we have malloc.h?]))

AC_OUTPUT([
Makefile
comoro/Makefile
libcomoroclient/Makefile
])
